package com.sxfq.loansupermarket.backsystem.modular.refunds.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.IntegralMall.service.MemberProductService;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.CustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.ScoreDealInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.service.ICustomerService;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.CheckoutMethodInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.PayServiceInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.PayServicedInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.VipLevelInfo;
import com.sxfq.loansupermarket.backsystem.modular.refunds.dao.RechargeDao;
import com.sxfq.loansupermarket.backsystem.modular.refunds.entity.QueryInfo;
import com.sxfq.loansupermarket.backsystem.modular.refunds.entity.RechargeInfo;
import com.sxfq.loansupermarket.backsystem.modular.refunds.entity.RefundInfo;
import com.sxfq.loansupermarket.backsystem.modular.refunds.entity.SearchRechargeInfo;
import com.sxfq.loansupermarket.backsystem.modular.refunds.service.IRechargeService;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.PaymentInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
@Transactional
public class RechargeServiceImpl implements IRechargeService {
    @Autowired
    private RechargeDao dao;
    
    @Autowired
    ICustomerService customerService;

    @Autowired
    private MemberProductService memberProductService;
    
    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<RechargeInfo> selectPageListRechargeInfo(Page<RechargeInfo> page, SearchRechargeInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        String tableHead = searchInfo.getAbbreviation();
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDealStatus())) {
            params.put("dealStatus", searchInfo.getDealStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerStatus())) {
            params.put("customerStatus", searchInfo.getCustomerStatus());
        }
        if (!"请选择".equals(searchInfo.getOneChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getOneChannel())) {
                params.put("oneChannel", searchInfo.getOneChannel());
            }
        }
        if (!"请选择".equals(searchInfo.getTwoChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getTwoChannel())) {
                params.put("twoChannel", searchInfo.getTwoChannel());
            }
        }
        if (!"请选择".equals(searchInfo.getThrChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getThrChannel())) {
                params.put("thrChannel", searchInfo.getThrChannel());
            }
        }
        params.put("tableName", tableHead +"customer_info");
        params.put("tableName1", tableHead + "deal_info");
        params.put("tableName2", tableHead + "recharge_info");
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPageRechargeInfo(page, params);
    }

    public String checkOut(String reservationsIdx, String customerId, String reservationsId, String[] str, String payorrefund, String orthodontics, double totleServiceActualPay1,
                        String operationTime, String getTime){
        String time = DateTimeKit.now();
        String sql2 = " select max(document_number) as documentNumber from " + ShiroKit.getUser().getRecharge_info() + " limit 1";
        RefundInfo refundInfo1 = sqlMapper.selectOne(sql2, RefundInfo.class);
        String documentNumber = "";
        Double documentNumbers2 = 0.00;
        if (refundInfo1 == null) {
        } else {
            documentNumbers2 = Double.parseDouble(refundInfo1.getDocumentNumber());
        }
        Double documentNumbers = documentNumbers2 + 1;
        if (documentNumbers < 10) {
            documentNumber = "000000" + documentNumbers;
        } else if (documentNumbers >= 10 && documentNumbers < 100) {
            documentNumber = "00000" + documentNumbers;
        } else if (documentNumbers >= 100 && documentNumbers < 1000) {
            documentNumber = "0000" + documentNumbers;
        } else if (documentNumbers >= 1000 && documentNumbers < 10000) {
            documentNumber = "000" + documentNumbers;
        } else if (documentNumbers >= 10000 && documentNumbers < 100000) {
            documentNumber = "00" + documentNumbers;
        } else if (documentNumbers >= 100000 && documentNumbers < 1000000) {
            documentNumber = "0" + documentNumbers;
        } else if (documentNumbers >= 1000000) {
            documentNumber = "" + documentNumbers;
        }
        documentNumber = documentNumber.substring(0, documentNumber.length() - 2);
        String times = time;
        if (operationTime.equals(getTime)) {
            times = operationTime;
        }
        String rechargeTime = DateTimeKit.now();
        String checkoutMethodName = "";
        double amountOfBalance = 0.00;
        String remarks = "";
        String selectsql1 = "select * from " + ShiroKit.getUser().getDeal_info() + " t1 where 1=1 " +
                " and t1.get_time='" + getTime + "' and t1.reservations_id='" + reservationsId + "'";
        List<PayServicedInfo> dealInfoList = sqlMapper.selectList(selectsql1, PayServicedInfo.class);
        String sql11 = "select * from " + ShiroKit.getUser().getCustomer_info() + " where arrival_status!=1 and arrival_status!=6 and arrival_status!=7 and arrival_status!=8 and arrival_status!=9 and arrival_status!=10 and arrival_time is not null " +
                " and reservations_id='" + reservationsId + "' limit 0,1 ";
        CustomerInfo customerInfo = sqlMapper.selectOne(sql11, CustomerInfo.class);
        if (dealInfoList.size() == 0) {
            return "开单项目已更改，请联系开单人重新结账！";
        } else {
            if ("已结账".equals(dealInfoList.get(0).getDealStatus())) {
                return "此单已结账！";
            }
            if ("结账".equals(payorrefund) || "消费".equals(payorrefund) || "还欠款".equals(payorrefund)) {
                for (PayServicedInfo dealoList : dealInfoList) {//遍历当前项目
                    Random rand = new Random();
                    int randomNumber = rand.nextInt(999999);
                    String id = randomNumber + operationTime;
                    Double sp = dealoList.getServiceActualPay() + dealoList.getDepositAmount();
                    //有实收为0 （欠款，用储值抵了，0元项目） 折扣为0,送的项目
                    if (dealoList.getServiceActualPay() == 0.00 && (dealoList.getDeductionAmount() != 0 || dealoList.getDepositAmount() != 0 || dealoList.getWholeDiscount() == 00000000.00)) {
                        String sql = " INSERT INTO `" + ShiroKit.getUser().getRecharge_info() + "` (`id`, `status`, `customer_id`, `reservations_id`, " +
                                "  `recharge_people`,`checkout_method_id`, `checkout_method`, `document_number`,deal_id " +
                                " ,`actual_account_balance`, `recharge_time`, `operation_time`, " +
                                "  `remarks`, `is_record`, `id2`,deduction_amount,deduction_type,hospital_performance,section_performance) values  " +
                                " ('" + id + "',1,'" + customerId + "','" + reservationsId + "', " +
                                " '" + ShiroKit.getUser().getAccount() +
                                "',(select id from checkout_method_info where checkout_method='现金')"
                                + ",'现金', '" + documentNumber + "' ,'" + dealoList.getId() + "'," +
                                " " + dealoList.getServiceActualPay() + ", '" + rechargeTime + "', '" + times + "', " +
                                " '" + remarks + "','是', '" + dealoList.getId2() + "','" + dealoList.getDeductionAmount() + "'," +
                                "'" + dealoList.getDeductionType() + "'," + dealoList.getServiceActualPay() + "," + dealoList.getServiceActualPay() + ") ";
                        sqlMapper.insert(sql);
                    } else {
                        continue;
                    }
                }
                checkout:
                for (int i = 0; i < str.length; i++) {//收费方式
                    checkoutMethodName = str[i].split("#")[0];
                    amountOfBalance = Double.parseDouble(str[i].split("#")[1]);
                    int index = str[i].indexOf("#");
                    index = str[i].indexOf("#", index + 1);
                    remarks = str[i].substring(index).replace("& #40;", "").replace("& #41;", "").replace("#", "");
                    deal:
                    for (PayServicedInfo dealoList : dealInfoList) {//项目  && dealoList.getServiceActualPay() > 0.00
                        Random rand = new Random();
                        int randomNumber = rand.nextInt(999999);
                        String id = randomNumber + operationTime;
                        if (amountOfBalance == dealoList.getServiceActualPay()) {//收费方式等于项目金额
                            String sql = " INSERT INTO `" + ShiroKit.getUser().getRecharge_info() + "` (`id`, `status`, `customer_id`, `reservations_id`, " +
                                    "  `recharge_people`,`checkout_method_id`, equation,`checkout_method`, `document_number`,deal_id " +
                                    " ,`actual_account_balance`, `recharge_time`, `operation_time`, " +
                                    "  `remarks`, `is_record`, `id2`,deduction_amount,deduction_type,hospital_performance,section_performance) values  " +
                                    " ('" + id + "',1,'" + customerId + "','" + reservationsId + "', " +
                                    " '" + ShiroKit.getUser().getAccount() +
                                    "',(select id from checkout_method_info where checkout_method='" + checkoutMethodName + "')," +
                                    "(select equation from checkout_method_info where checkout_method='" + checkoutMethodName + "'),'"
                                    + checkoutMethodName + "', '" + documentNumber + "' ,'" + dealoList.getId() + "'," +
                                    " " + amountOfBalance + ", '" + rechargeTime + "', '" + times + "', " +
                                    " '" + remarks + "','是', '" + dealoList.getId2() + "','" + dealoList.getDeductionAmount() + "'," +
                                    "'" + dealoList.getDeductionType() + "'," +
                                    "if((select charge_bill_is_inCash from checkout_method_info where checkout_method='" + checkoutMethodName + "')=0,0," + amountOfBalance + ")," +
                                    "if((select charge_bill_is_inCash from checkout_method_info where checkout_method='" + checkoutMethodName + "')=0,0," + amountOfBalance + ") ) ";
                            amountOfBalance = amountOfBalance - dealoList.getServiceActualPay();
                            dealoList.setServiceActualPay(dealoList.getServiceActualPay() - amountOfBalance);
                            sqlMapper.insert(sql);
                        } else if (amountOfBalance > dealoList.getServiceActualPay()) {//收费方式大于项目金额
                            String sql = " INSERT INTO `" + ShiroKit.getUser().getRecharge_info() + "` (`id`, `status`, `customer_id`, `reservations_id`, " +
                                    "  `recharge_people`, `checkout_method_id`,equation,`checkout_method`, `document_number`,deal_id " +
                                    " ,`actual_account_balance`, `recharge_time`, `operation_time`, " +
                                    "  `remarks`, `is_record`, `id2`,deduction_amount,deduction_type,hospital_performance,section_performance) values  " +
                                    "( '" + id + "',1,'" + customerId + "','" + reservationsId + "', " +
                                    " '" + ShiroKit.getUser().getAccount() +
                                    "',(select id from checkout_method_info where checkout_method='" + checkoutMethodName + "')," +
                                    "(select equation from checkout_method_info where checkout_method='" + checkoutMethodName + "'),'"
                                    + checkoutMethodName + "', '" + documentNumber + "' ,'" + dealoList.getId() + "'," +
                                    " " + dealoList.getServiceActualPay() + ", '" + rechargeTime + "', '" + times + "', " +
                                    " '" + remarks + "','是', '" + dealoList.getId2() + "','" + dealoList.getDeductionAmount() + "'," +
                                    "'" + dealoList.getDeductionType() + "'," +
                                    "if((select charge_bill_is_inCash from checkout_method_info where checkout_method='" + checkoutMethodName + "')=0,0," + dealoList.getServiceActualPay() + ")," +
                                    "if((select charge_bill_is_inCash from checkout_method_info where checkout_method='" + checkoutMethodName + "')=0,0," + dealoList.getServiceActualPay() + ") ) ";
                            sqlMapper.insert(sql);
                            amountOfBalance = amountOfBalance - dealoList.getServiceActualPay();
                            dealoList.setServiceActualPay(0.00);
                            continue deal;
                        } else if (amountOfBalance < dealoList.getServiceActualPay()) {//收费方式小于项目金额
                            String sql = " INSERT INTO `" + ShiroKit.getUser().getRecharge_info() + "` (`id`, `status`, `customer_id`, `reservations_id`, " +
                                    "  `recharge_people`,`checkout_method_id`, equation,`checkout_method`, `document_number`,deal_id " +
                                    " ,`actual_account_balance`, `recharge_time`, `operation_time`, " +
                                    "  `remarks`, `is_record`, `id2`,deduction_amount,deduction_type,hospital_performance,section_performance) values  " +
                                    "( '" + id + "',1,'" + customerId + "','" + reservationsId + "', " +
                                    " '" + ShiroKit.getUser().getAccount() +
                                    "',(select id from checkout_method_info where checkout_method='" + checkoutMethodName + "')," +
                                    "(select equation from checkout_method_info where checkout_method='" + checkoutMethodName + "'),'"
                                    + checkoutMethodName + "', '" + documentNumber + "' ,'" + dealoList.getId() + "'," +
                                    " " + amountOfBalance + ", '" + rechargeTime + "', '" + times + "', " +
                                    " '" + remarks + "','是', '" + dealoList.getId2() + "','" + dealoList.getDeductionAmount() + "'," +
                                    "'" + dealoList.getDeductionType() + "'," +
                                    "if((select charge_bill_is_inCash from checkout_method_info where checkout_method='" + checkoutMethodName + "')=0,0," + amountOfBalance + ")," +
                                    "if((select charge_bill_is_inCash from checkout_method_info where checkout_method='" + checkoutMethodName + "')=0,0," + amountOfBalance + ") ) ";
                            sqlMapper.insert(sql);
                            dealoList.setServiceActualPay(dealoList.getServiceActualPay() - amountOfBalance);
                            continue checkout;
                        }
                    }
                }
            } else if ("退款".equals(payorrefund)) {
                Double tservice = 0 - totleServiceActualPay1;
                String sqlt = "select id from " + ShiroKit.getUser().getDeal_info() + " where deal_status='已开单' and " +
                        "operation_time='" + operationTime + "' and reservations_id='" + reservationsId + "' and is_do='3' limit 1";
                PayServiceInfo dealInfot = sqlMapper.selectOne(sqlt, PayServiceInfo.class);
                if (dealInfot != null) {
                    String sqlu = "delete from " + ShiroKit.getUser().getDeal_info() + " where deal_status='已开单' " +
                            " and operation_time='" + operationTime + "' and reservations_id='" + reservationsId + "' and is_do='3'";
                    sqlMapper.delete(sqlu);
                }
                //遍历当前项目
                for (PayServicedInfo dealoList : dealInfoList) {//项目
                    Random rand = new Random();
                    int randomNumber = rand.nextInt(999999);
                    String id = randomNumber + operationTime;
                    Double sp = dealoList.getServiceActualPay() + dealoList.getDepositAmount();
                    //退款的时候改变数量
                    String sqlu = "update " + ShiroKit.getUser().getDeal_info() + " set pay_service_num='" + dealoList.getPayServiceNum() + "' where id2='" + dealoList.getId2() + "';";
                    sqlMapper.update(sqlu);
                    //退款项目中有储值
                    if (dealoList.getDepositAmount() != 0.00) {
                        String sql = " INSERT INTO `" + ShiroKit.getUser().getRecharge_info() + "` (`id`, `status`, `customer_id`, `reservations_id`, " +
                                "  `recharge_people`,`checkout_method_id`, `checkout_method`, `document_number`,deal_id " +
                                " ,`actual_account_balance`, `recharge_time`, `operation_time`, " +
                                "  `remarks`, `is_record`, `id2`,deduction_amount,deduction_type,hospital_performance,section_performance," +
                                " isEarnes,tc_name) values  " +
                                " ('" + id + "',1,'" + customerId + "','" + reservationsId + "', " +
                                " '" + ShiroKit.getUser().getAccount() +
                                "',(select id from checkout_method_info where checkout_method='储值抵扣')"
                                + ",'储值抵扣', '" + documentNumber + "' ,'" + dealoList.getId() + "'," +
                                " " + dealoList.getDepositAmount() + ", '" + rechargeTime + "', '" + times + "', " +
                                " '储值退款','是', '" + dealoList.getId2() + "','" + dealoList.getDeductionAmount() + "','" + dealoList.getDeductionType() + "'," +
                                "" + dealoList.getDepositAmount() + "," + dealoList.getDepositAmount() + ",'5','" + dealoList.getTcName() + "') ";
                        sqlMapper.insert(sql);
                        Double depositAmount = -dealoList.getDepositAmount();
                        String sqlIN = " INSERT INTO `" + ShiroKit.getUser().getRecharge_info() + "` (`id`, `status`, `customer_id`, `reservations_id`, " +
                                "  `recharge_people`,`checkout_method_id`, `checkout_method`, `document_number`,deal_id " +
                                " ,`actual_account_balance`, `recharge_time`, `operation_time`, " +
                                "  `remarks`, `is_record`, `id2`,deduction_amount,deduction_type,hospital_performance,section_performance," +
                                " isEarnes,tc_name) values  " +
                                " ('" + id + "+1',1,'" + customerId + "','" + reservationsId + "', " +
                                " '" + ShiroKit.getUser().getAccount() +
                                "',(select id from checkout_method_info where checkout_method='储值抵扣')"
                                + ",'储值抵扣', '" + documentNumber + "' ,''," +
                                "" + depositAmount + ", '" + rechargeTime + "', '" + times + "', " +
                                " '储值退款','是', '" + dealoList.getId2() + "','" + dealoList.getDeductionAmount() + "','" + dealoList.getDeductionType() + "'," +
                                "" + depositAmount + "," + depositAmount + ",'4','" + dealoList.getTcName() + "') ";
                        sqlMapper.insert(sqlIN);
                        String sql2r = " INSERT INTO reserveMoney_deal (`status`,recharge_deal_id ,`reservations_id`, `customer_id`," +
                                "  `recharge_people`,`checkout_method_id`, `checkout_method`, " +
                                "  `actual_account_balance`, `recharge_time`, `operation_time`, " +
                                "  `remarks`, `is_record`,tc_name,add_store,mode,deal_id) " +
                                " select `status`,recharge_deal_id ,`reservations_id`, `customer_id`," +
                                " `recharge_people`,`checkout_method_id`, `checkout_method`, " +
                                " '" + depositAmount + "', `recharge_time`, `operation_time`," +
                                " '储值消费退回', `is_record`,tc_name,add_store,'充值',deal_id from reserveMoney_deal " +
                                " where recharge_deal_id='" + dealoList.getId2() + "' and mode='消费'";
                        sqlMapper.insert(sql2r);
                    } else {
                        continue;
                    }
                }
                //遍历当前项目
                for (PayServicedInfo dealoList : dealInfoList) {//项目
                    Random rand = new Random();
                    int randomNumber = rand.nextInt(999999);
                    String id = randomNumber + operationTime;
                    //退款项目中有返现
                    String sqlch = "select * from checkout_method_info where id='" + dealoList.getDeductionType() + "'";
                    CheckoutMethodInfo checkoutMethodInfos = new CheckoutMethodInfo();
                    CheckoutMethodInfo checkoutMethodInfo = sqlMapper.selectOne(sqlch, CheckoutMethodInfo.class);
                    if (checkoutMethodInfo == null) {
                        checkoutMethodInfos.setType("00");
                        checkoutMethodInfo = checkoutMethodInfos;
                    }
                    if (dealoList.getDeductionAmount() != 0.00 && "1".equals(checkoutMethodInfo.getType())) {
                        //插入到结账表一进一退
                        String sql = " INSERT INTO `" + ShiroKit.getUser().getRecharge_info() + "` (`id`, `status`, `customer_id`, `reservations_id`, " +
                                "  `recharge_people`,`checkout_method_id`, `checkout_method`, `document_number` " +
                                " ,`actual_account_balance`, `recharge_time`, `operation_time`, " +
                                "  `remarks`, `is_record`, `id2`,deal_id,isEarnes,tc_name) values  " +
                                "( '" + id + "',1,'" + customerId + "','" + reservationsId + "', " +
                                " '" + ShiroKit.getUser().getAccount() +
                                "','" + checkoutMethodInfo.getId() + "','" + checkoutMethodInfo.getCheckoutMethod() + "', '" + documentNumber + "' ," +
                                " " + dealoList.getDeductionAmount() + ", '" + rechargeTime + "', '" + times + "', " +
                                " '返现退款" + dealoList.getDeductionAmount() + "','是', '" + dealoList.getId2() + "','" + dealoList.getId() + "',3,'" + dealoList.getTcName() + "') ";
                        sqlMapper.insert(sql);
                        BigDecimal b1 = new BigDecimal(Double.parseDouble(dealoList.getDeductionAmount().toString().substring(1)));
                        BigDecimal b2 = new BigDecimal(Double.parseDouble(ShiroKit.getUser().getProportional()));
                        Double jfbalance = b1.divide(b2, 2, RoundingMode.HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                        VipLevelInfo vipLevelInfo = memberProductService.getVipLevel101(customerInfo.getReservationsId());
                        String jfbalancesql1 = "select if(sum(score_balance) is null,0,sum(score_balance)) as scoreBalances from return_cash_info " +
                                "where reservations_id='" + customerInfo.getReservationsId() + "' and mode='返现';";
                        ScoreDealInfo scoreDealInfo1 = sqlMapper.selectOne(jfbalancesql1, ScoreDealInfo.class);
//                        Double allReturnCash = vipLevelInfo.getAddMoney() - scoreDealInfo1.getScoreBalances();
                        String upda = "update " + ShiroKit.getUser().getCustomer_info() + " set jfbalance=jfbalance+'" + jfbalance + "' where reservations_id='" + reservationsId + "'";
                        sqlMapper.update(upda);
                    } else {
                        continue;
                    }
                }
                //遍历当前项目
                for (PayServicedInfo dealoList : dealInfoList) {//项目
                    Random rand = new Random();
                    int randomNumber = rand.nextInt(999999);
                    String id = randomNumber + operationTime;
                    //判断是否使用了优惠券
                    String sql1 = "select * from checkout_method_info where id='" + dealoList.getDeductionType() + "' limit 1";
                    CheckoutMethodInfo checkoutMethodInfo = sqlMapper.selectOne(sql1, CheckoutMethodInfo.class);
                    if (checkoutMethodInfo != null) {   //0:正常方式，1:返现，2:优惠券
                        if (dealoList.getDeductionAmount() != 0.00 && ("2").equals(checkoutMethodInfo.getType())) {
                            //插入到结账表退
                            String sql = " INSERT INTO `" + ShiroKit.getUser().getRecharge_info() + "` (`id`, `status`, `customer_id`, `reservations_id`, " +
                                    "  `recharge_people`,`checkout_method_id`, `checkout_method`, `document_number` " +
                                    " ,`actual_account_balance`, `recharge_time`, `operation_time`, " +
                                    "  `remarks`, `is_record`, `id2`,deal_id,isEarnes) values  " +
                                    "( '" + id + "',1,'" + customerId + "','" + reservationsId + "', " +
                                    " '" + ShiroKit.getUser().getAccount() +
                                    "','" + checkoutMethodInfo.getId() + "','" + checkoutMethodInfo.getCheckoutMethod() + "', '" + documentNumber + "' ," +
                                    " " + dealoList.getDeductionAmount() + ", '" + rechargeTime + "', '" + times + "', " +
                                    " '优惠券退款" + dealoList.getDeductionAmount() + "','是', '" + dealoList.getId2() + "','" + dealoList.getId() + "',9) ";
                            sqlMapper.insert(sql);
                        } else {
                            continue;
                        }
                    } else {
                        continue;
                    }
                }
                checkout:
                for (int i = 0; i < str.length; i++) {//收费方式
                    checkoutMethodName = str[i].split("#")[0];
                    amountOfBalance = Double.parseDouble(str[i].split("#")[1]);
                    int index = str[i].indexOf("#");
                    index = str[i].indexOf("#", index + 1);
                    remarks = str[i].substring(index).replace("& #40;", "").replace("& #41;", "").replace("#", "");
                    deal:
                    for (PayServicedInfo dealoList : dealInfoList) {//项目  && dealoList.getServiceActualPay() > 0.00
                        Random rand = new Random();
                        int randomNumber = rand.nextInt(999999);
                        String id = randomNumber + operationTime;
                        if (amountOfBalance == dealoList.getServiceActualPay()) {//收费方式等于项目金额
                            String sql = " INSERT INTO `" + ShiroKit.getUser().getRecharge_info() + "` (`id`, `status`, `customer_id`, `reservations_id`, " +
                                    "  `recharge_people`,`checkout_method_id`, equation,`checkout_method`, `document_number`,deal_id " +
                                    " ,`actual_account_balance`, `recharge_time`, `operation_time`, " +
                                    "  `remarks`, `is_record`, `id2`,deduction_amount,deduction_type,tc_name,hospital_performance,section_performance) values  " +
                                    " ('" + id + "',1,'" + customerId + "','" + reservationsId + "', " +
                                    " '" + ShiroKit.getUser().getAccount() +
                                    "',(select id from checkout_method_info where checkout_method='" + checkoutMethodName + "')," +
                                    "(select equation from checkout_method_info where checkout_method='" + checkoutMethodName + "'),'"
                                    + checkoutMethodName + "', '" + documentNumber + "' ,'" + dealoList.getId() + "'," +
                                    " " + amountOfBalance + ", '" + rechargeTime + "', '" + times + "', " +
                                    " '" + remarks + "','是', '" + dealoList.getId2() + "'," +
                                    "'" + dealoList.getDeductionAmount() + "','" + dealoList.getDeductionType() + "','" + dealoList.getTcName() + "'," +
                                    "if((select charge_bill_is_inCash from checkout_method_info where checkout_method='" + checkoutMethodName + "')=0,0," + amountOfBalance + ")," +
                                    "if((select charge_bill_is_inCash from checkout_method_info where checkout_method='" + checkoutMethodName + "')=0,0," + amountOfBalance + ") ) ";
                            amountOfBalance = amountOfBalance - dealoList.getServiceActualPay();
                            dealoList.setServiceActualPay(dealoList.getServiceActualPay() - amountOfBalance);
                            sqlMapper.insert(sql);
                        } else if (amountOfBalance < dealoList.getServiceActualPay()) {//收费方式大于项目金额
                            String sql = " INSERT INTO `" + ShiroKit.getUser().getRecharge_info() + "` (`id`, `status`, `customer_id`, `reservations_id`, " +
                                    "  `recharge_people`, `checkout_method_id`,equation,`checkout_method`, `document_number`,deal_id " +
                                    " ,`actual_account_balance`, `recharge_time`, `operation_time`, " +
                                    "  `remarks`, `is_record`, `id2`,deduction_amount,deduction_type,tc_name,hospital_performance,section_performance) values  " +
                                    "( '" + id + "',1,'" + customerId + "','" + reservationsId + "', " +
                                    " '" + ShiroKit.getUser().getAccount() +
                                    "',(select id from checkout_method_info where checkout_method='" + checkoutMethodName + "')," +
                                    "(select equation from checkout_method_info where checkout_method='" + checkoutMethodName + "'),'"
                                    + checkoutMethodName + "', '" + documentNumber + "' ,'" + dealoList.getId() + "'," +
                                    " " + dealoList.getServiceActualPay() + ", '" + rechargeTime + "', '" + times + "', " +
                                    " '" + remarks + "','是', '" + dealoList.getId2() + "'," +
                                    "'" + dealoList.getDeductionAmount() + "','" + dealoList.getDeductionType() + "','" + dealoList.getTcName() + "'," +
                                    "if((select charge_bill_is_inCash from checkout_method_info where checkout_method='" + checkoutMethodName + "')=0,0," + dealoList.getServiceActualPay() + ")," +
                                    "if((select charge_bill_is_inCash from checkout_method_info where checkout_method='" + checkoutMethodName + "')=0,0," + dealoList.getServiceActualPay() + ") ) ";
                            sqlMapper.insert(sql);
                            amountOfBalance = amountOfBalance - dealoList.getServiceActualPay();
                            dealoList.setServiceActualPay(0.00);
                            continue deal;
                        } else if (amountOfBalance > dealoList.getServiceActualPay()) {//收费方式小于项目金额
                            String sql = " INSERT INTO `" + ShiroKit.getUser().getRecharge_info() + "` (`id`, `status`, `customer_id`, `reservations_id`, " +
                                    "  `recharge_people`,`checkout_method_id`, equation,`checkout_method`, `document_number`,deal_id " +
                                    " ,`actual_account_balance`, `recharge_time`, `operation_time`, " +
                                    "  `remarks`, `is_record`, `id2`,deduction_amount,deduction_type,tc_name,hospital_performance,section_performance) values  " +
                                    "( '" + id + "',1,'" + customerId + "','" + reservationsId + "', " +
                                    " '" + ShiroKit.getUser().getAccount() +
                                    "',(select id from checkout_method_info where checkout_method='" + checkoutMethodName + "')," +
                                    "(select equation from checkout_method_info where checkout_method='" + checkoutMethodName + "'),'"
                                    + checkoutMethodName + "', '" + documentNumber + "' ,'" + dealoList.getId() + "'," +
                                    " " + amountOfBalance + ", '" + rechargeTime + "', '" + times + "', " +
                                    " '" + remarks + "','是', '" + dealoList.getId2() + "'," +
                                    "'" + dealoList.getDeductionAmount() + "','" + dealoList.getDeductionType() + "','" + dealoList.getTcName() + "'," +
                                    "if((select charge_bill_is_inCash from checkout_method_info where checkout_method='" + checkoutMethodName + "')=0,0," + amountOfBalance + ")," +
                                    "if((select charge_bill_is_inCash from checkout_method_info where checkout_method='" + checkoutMethodName + "')=0,0," + amountOfBalance + ") ) ";
                            sqlMapper.insert(sql);
                            dealoList.setServiceActualPay(dealoList.getServiceActualPay() - amountOfBalance);
                            continue checkout;
                        }
                    }
                }
            }
            //修改医生业绩表开单状态
            String updateSql = "update " + ShiroKit.getUser().getTriage_performance() + " a set a.deal_status='已结账' where a.customer_id = '" + customerId + "'";
            String updateSqll = "update doctor_change_price a set a.status=1 where a.customer_id = '" + customerId + "'";
            sqlMapper.update(updateSql);
            sqlMapper.update(updateSqll);
            String sqlde1 = "select id from " + ShiroKit.getUser().getRecharge_info() + " where reservations_id='" + reservationsId + "' group by id2;";
            List<PayServicedInfo> list11 = sqlMapper.selectList(sqlde1, PayServicedInfo.class);//查询当前记录
            String ids = "";
            if (list11.size() > 0) {
                for (PayServicedInfo li : list11) {
                    ids += "','" + li.getId();
                }
                String updatede = "update " + ShiroKit.getUser().getRecharge_info() + " set deduction_type='',deduction_amount='0.00' " +
                        " Where id not in ('" + ids.substring(3) + "') and reservations_id='" + reservationsId + "'";
                sqlMapper.update(updatede);
            }
            if (!operationTime.equals(getTime)) {
                this.updateGetTime(reservationsId, getTime, time);
                getTime = time;
            }
            String sqlCustau = "select a.is_do as isDo from " + ShiroKit.getUser().getDeal_info() + " a left join " + ShiroKit.getUser().getPay_service_info() + " b on " +
                    " (a.pay_service_id=b.id) left join " + ShiroKit.getUser().getCustomer_info() + " c on(a.customer_id=c.id)" +
                    " where a.operation_time='" + operationTime + "' and a.get_time='" + getTime + "' " +
                    " and a.reservations_id='" + reservationsId + "'";
            List<PayServicedInfo> openct = sqlMapper.selectList(sqlCustau, PayServicedInfo.class);//查询当前记录
            Integer isdo = 0;
            for (PayServicedInfo inf : openct) {
                isdo = inf.getIsDo();
            }
            //1:不做了,2:继续做,3:还欠款,4:退款,5:消费,6:作废
            if (isdo == 1 || isdo == 2 || isdo == 4 || isdo == 3) {    //退款、还欠款继承正畸编号
                //遍历当前项目
                for (PayServicedInfo dealoList : dealInfoList) {//项目
                    //判断改结账项目是否需要正畸编号
                    String delinf = "select id,ifnull(orthodontic_number,'') as orthodonticNumber from " + ShiroKit.getUser().getDeal_info() + " where id='" + dealoList.getId2() + "' limit 1";
                    PayServiceInfo dei = sqlMapper.selectOne(delinf, PayServiceInfo.class);
                    if (dei != null) {
                        String sqlup = "update " + ShiroKit.getUser().getDeal_info() + " set orthodontic_number='" + dei.getOrthodonticNumber() + "'" +
                                " where id='" + dealoList.getId() + "'";
                        sqlMapper.update(sqlup);
                    }
                }
            }
            if (isdo == 5 || isdo == 3) {    //结账 修改客户开单状态
                if (isdo == 5) {
                    String sqlup = "update " + ShiroKit.getUser().getDeal_info() + " set orthodontic_number='" + orthodontics + "'" +
                            " where reservations_id='" + reservationsId + "' and operation_time='" + operationTime + "' and get_time='" + getTime + "'";
                    sqlMapper.update(sqlup);
                }
                //查询当前成交的项目是不是算已结账的项目
                String sqldeal = "select a.id from " + ShiroKit.getUser().getDeal_info() + " a left join " + ShiroKit.getUser().getPay_service_info() + " b on (a.pay_service_id=b.id) " +
                        " where a.operation_time='" + operationTime + "' and a.get_time='" + getTime + "' " +
                        " and a.reservations_id='" + reservationsId + "' and b.is_deal=1";
                List<PayServicedInfo> listdeal = sqlMapper.selectList(sqldeal, PayServicedInfo.class);//查询当前记录
                //结账修改客户状态表为已结账
                if (listdeal.size() > 0) {
                    String sqldist = "select a.id from distinction_customer_status a where a.reservations_id='" + reservationsId + "' and deal_status='未成交'";
                    List<PayServicedInfo> sqldistl = sqlMapper.selectList(sqldist, PayServicedInfo.class);
                    if (sqldistl.size() > 0) {  //之前未成交，本次成交了，修改时间状态
                        String up1 = "update distinction_customer_status set deal_status='已成交',update_time='" + DateTimeKit.now() + "' where reservations_id='" + reservationsId + "' " +
                                " and status=1  and arrival_store='" + ShiroKit.getUser().getOutpatientName() + "';";
                        sqlMapper.update(up1);
                    }
                    String up3 = "update " + ShiroKit.getUser().getCustomer_info() + " set isdeal_status='已成交' where id='" + customerId + "'";
                    sqlMapper.update(up3);
                }
                String updatesql3 = "update " + ShiroKit.getUser().getCustomer_info() + " set deal_status='已结账' where id='" + customerId + "'  ";
                sqlMapper.update(updatesql3);
            }
            if (isdo == 5 || isdo == 3) {    //结账 补欠款  返现和优惠券变动
                //遍历当前项目
                for (PayServicedInfo dealoList : dealInfoList) {//项目
                    Random rand = new Random();
                    int randomNumber = rand.nextInt(999999);
                    String id = randomNumber + operationTime;
                    //判断是否使用了优惠券
                    String sql1 = "select * from checkout_method_info where id='" + dealoList.getDeductionType() + "' limit 1";
                    CheckoutMethodInfo checkoutMethodInfo = sqlMapper.selectOne(sql1, CheckoutMethodInfo.class);
                    if (checkoutMethodInfo != null) {   //0:正常方式，1:返现，2:优惠券
                        if (("2").equals(checkoutMethodInfo.getType()) && dealoList.getDeductionAmount() > 0.00) {
                            //插入到结账表一进一退
                            String sql = " INSERT INTO `" + ShiroKit.getUser().getRecharge_info() + "` (`id`, `status`, `customer_id`, `reservations_id`, " +
                                    "  `recharge_people`,`checkout_method_id`, `checkout_method`, `document_number` " +
                                    " ,`actual_account_balance`, `recharge_time`, `operation_time`, " +
                                    "  `remarks`, `is_record`, `id2`,deal_id,isEarnes) values  " +
                                    "( '" + id + "',1,'" + customerId + "','" + reservationsId + "', " +
                                    " '" + ShiroKit.getUser().getAccount() +
                                    "','" + checkoutMethodInfo.getId() + "','" + checkoutMethodInfo.getCheckoutMethod() + "', '" + documentNumber + "' ," +
                                    " " + dealoList.getDeductionAmount() + ", '" + rechargeTime + "', '" + times + "', " +
                                    " '优惠券抵扣" + dealoList.getDeductionAmount() + "','是', '" + dealoList.getId2() + "','" + dealoList.getId() + "',9) ";
                            sqlMapper.insert(sql);
                            String sqlt = " INSERT INTO `" + ShiroKit.getUser().getRecharge_info() + "` (`id`, `status`, `customer_id`, `reservations_id`, " +
                                    "  `recharge_people`,`checkout_method_id`, `checkout_method`, `document_number` " +
                                    " ,`actual_account_balance`, `recharge_time`, `operation_time`, " +
                                    "  `remarks`, `is_record`, `id2`,isEarnes) values  " +
                                    "( '" + id + "+1',1,'" + customerId + "','" + reservationsId + "', " +
                                    " '" + ShiroKit.getUser().getAccount() +
                                    "','" + checkoutMethodInfo.getId() + "','" + checkoutMethodInfo.getCheckoutMethod() + "', '" + documentNumber + "' ," +
                                    " -" + dealoList.getDeductionAmount() + ", '" + rechargeTime + "', '" + times + "', " +
                                    " '优惠券抵扣-" + dealoList.getDeductionAmount() + "','是', '" + dealoList.getId2() + "',8) ";
                            sqlMapper.insert(sqlt);
                        } else {
                            continue;
                        }
                    } else {
                        continue;
                    }
                }
                //遍历当前项目
                for (PayServicedInfo dealoList : dealInfoList) {//项目
                    Random rand = new Random();
                    int randomNumber = rand.nextInt(999999);
                    String id = randomNumber + operationTime;
                    //判断是否使用了返现
                    String sql1 = "select * from checkout_method_info where id='" + dealoList.getDeductionType() + "' limit 1";
                    CheckoutMethodInfo checkoutMethodInfo = sqlMapper.selectOne(sql1, CheckoutMethodInfo.class);
                    if (checkoutMethodInfo != null) {
                        if (("1").equals(checkoutMethodInfo.getType()) && dealoList.getDeductionAmount() > 0.00) {
                            //插入到结账表一进一退
                            String sql = " INSERT INTO `" + ShiroKit.getUser().getRecharge_info() + "` (`id`, `status`, `customer_id`, `reservations_id`, " +
                                    "  `recharge_people`,`checkout_method_id`, `checkout_method`, `document_number` " +
                                    " ,`actual_account_balance`, `recharge_time`, `operation_time`, " +
                                    "  `remarks`, `is_record`, `id2`,deal_id,isEarnes,tc_name) values  " +
                                    "( '" + id + "',1,'" + customerId + "','" + reservationsId + "', " +
                                    " '" + ShiroKit.getUser().getAccount() +
                                    "','" + checkoutMethodInfo.getId() + "','" + checkoutMethodInfo.getCheckoutMethod() + "', '" + documentNumber + "' ," +
                                    " " + dealoList.getDeductionAmount() + ", '" + rechargeTime + "', '" + times + "', " +
                                    " '返现抵扣" + dealoList.getDeductionAmount() + "','是', '" + dealoList.getId2() + "','" + dealoList.getId() + "',7,'" + dealoList.getTcName() + "') ";
                            sqlMapper.insert(sql);
                            String sqlt = " INSERT INTO `" + ShiroKit.getUser().getRecharge_info() + "` (`id`, `status`, `customer_id`, `reservations_id`, " +
                                    "  `recharge_people`,`checkout_method_id`, `checkout_method`, `document_number` " +
                                    " ,`actual_account_balance`, `recharge_time`, `operation_time`, " +
                                    "  `remarks`, `is_record`, `id2`,isEarnes,tc_name) values  " +
                                    "( '" + id + "+1',1,'" + customerId + "','" + reservationsId + "', " +
                                    " '" + ShiroKit.getUser().getAccount() +
                                    "','" + checkoutMethodInfo.getId() + "','" + checkoutMethodInfo.getCheckoutMethod() + "', '" + documentNumber + "' ," +
                                    " -" + dealoList.getDeductionAmount() + ", '" + rechargeTime + "', '" + times + "', " +
                                    " '返现抵扣-" + dealoList.getDeductionAmount() + "','是', '" + dealoList.getId2() + "',6,'" + dealoList.getTcName() + "') ";
                            sqlMapper.insert(sqlt);
                            Double allReturnCash = 0 - dealoList.getDeductionAmount();
                            String sqlinsert = "insert into return_cash_info (reservations_id,customer_name,customer_phone,score_balance,mode,remarks,add_people,add_time,add_store) values" +
                                    " ('" + customerInfo.getReservationsId() + "','" + customerInfo.getCustomerName() + "','" + customerInfo.getCustomerPhone() + "','" + allReturnCash + "','返现消费','做项目使用返现'," +
                                    " '" + ShiroKit.getUser().getAccount() + "','" + DateTimeKit.now() + "','" + ShiroKit.getUser().getOutpatientName() + "')";
                            sqlMapper.insert(sqlinsert);
                        } else {
                            continue;
                        }
                    } else {
                        continue;
                    }
                }
                //遍历当前项目  1:开单缴费，2:定金缴费，3:返现缴费，4:储值抵扣﹣，5:储值抵扣+，6:返现抵扣﹣，7:返现抵扣+，8:优惠券抵扣﹣，9:优惠券抵扣+
                for (PayServicedInfo dealoList : dealInfoList) {//项目
                    //判断是否使用了储值,插入到储值记录表
                    if (dealoList.getDepositAmount() > 0.00) {
                        Random rand = new Random();
                        int randomNumber = rand.nextInt(999999);
                        String id = randomNumber + operationTime;
                        if (reservationsIdx != null && reservationsIdx != "") {
                            dealoList.setReservationsId(reservationsIdx);
                        }
                        //插入到储值记录表
                        String sql2r = " INSERT INTO reserveMoney_deal ( `status`,recharge_deal_id ,`reservations_id`, " +
                                "  `recharge_people`,`checkout_method_id`, `checkout_method`, " +
                                "  `actual_account_balance`, `recharge_time`, `operation_time`, " +
                                "  `remarks`, `is_record`,tc_name,add_store,mode) values  " +
                                " (1,'" + dealoList.getId() + "','" + dealoList.getReservationsId() + "', " +
                                " '" + ShiroKit.getUser().getAccount() +
                                "',(select id from checkout_method_info where checkout_method='现金'),'现金' , -" + dealoList.getDepositAmount()
                                + ", '" + DateTimeKit.now() + "', '" + DateTimeKit.now() + "', " +
                                " '做项目使用储值','是','" + dealoList.getTcName() + "','" + ShiroKit.getUser().getOutpatientName() + "','消费') ";
                        sqlMapper.insert(sql2r);
                        //插入到结账表一进一退
                        String sql = " INSERT INTO `" + ShiroKit.getUser().getRecharge_info() + "` (`id`, `status`, `customer_id`, `reservations_id`, " +
                                "  `recharge_people`,`checkout_method_id`, `checkout_method`, `document_number` " +
                                " ,`actual_account_balance`, `recharge_time`, `operation_time`, " +
                                "  `remarks`, `is_record`, `id2`,hospital_performance,section_performance,deal_id,isEarnes,tc_name) values  " +
                                "( '" + id + "',1,'" + customerId + "','" + reservationsId + "', " +
                                " '" + ShiroKit.getUser().getAccount() +
                                "',(select id from checkout_method_info where checkout_method='储值抵扣'),'储值抵扣', '" + documentNumber + "' ," +
                                " " + dealoList.getDepositAmount() + ", '" + rechargeTime + "', '" + times + "', " +
                                " '储值抵扣" + dealoList.getDepositAmount() + "','是', '" + dealoList.getId2() + "','" + dealoList.getDepositAmount() + "','"
                                + dealoList.getDepositAmount() + "', '" + dealoList.getId() + "', 5,'" + dealoList.getTcName() + "') ";
                        sqlMapper.insert(sql);
                        String sql1 = " INSERT INTO `" + ShiroKit.getUser().getRecharge_info() + "` (`id`, `status`, `customer_id`, `reservations_id`, " +
                                "  `recharge_people`,`checkout_method_id`, `checkout_method`, `document_number` " +
                                " ,`actual_account_balance`, `recharge_time`, `operation_time`, " +
                                "  `remarks`, `is_record`, `id2`,isEarnes,tc_name,hospital_performance) values  " +
                                "( '" + id + "+1',1,'" + customerId + "','" + reservationsId + "', " +
                                " '" + ShiroKit.getUser().getAccount() +
                                "',(select id from checkout_method_info where checkout_method='储值抵扣'),'储值抵扣', '" + documentNumber + "' ," +
                                " -" + dealoList.getDepositAmount() + ", '" + rechargeTime + "', '" + times + "', " +
                                " '储值抵扣-" + dealoList.getDepositAmount() + "','是', '" + dealoList.getId2() + "', 4,'" + dealoList.getTcName() + "',-" + dealoList.getDepositAmount() + ") ";
                        sqlMapper.insert(sql1);
                    } else {
                        continue;
                    }
                }
                //查询本次使用优惠券和返现的抵扣金额
                String jfmoney = "select COALESCE(sum(t.deduction_amount),0.00) as totakDeductionAmount from " + ShiroKit.getUser().getDeal_info() +
                        " t left join checkout_method_info t2 on (t.deduction_type=t2.id) " +
                        " where t.reservations_id='" + reservationsId + "' and t.get_time='" + getTime + "' and t2.type='1';";
                RechargeInfo jfmone = sqlMapper.selectOne(jfmoney, RechargeInfo.class);
                BigDecimal b1 = new BigDecimal(Double.parseDouble(jfmone.getTotakDeductionAmount()));
                BigDecimal b2 = new BigDecimal(Double.parseDouble(ShiroKit.getUser().getProportional()));
                Double jfbalance = b1.divide(b2, 2, RoundingMode.HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                String sqlde = "update " + ShiroKit.getUser().getCustomer_info() + " set jfbalance=jfbalance-'" + jfbalance + "' " +
                        " where reservations_id='" + reservationsId + "' ";
                sqlMapper.update(sqlde);
            }
            //修改开单状态
            String updatesqld = "update " + ShiroKit.getUser().getDeal_info() + " set deal_status='已结账' " +
                    " where reservations_id='" + reservationsId + "' and operation_time='" + operationTime + "' and get_time='" + getTime + "'";
            sqlMapper.update(updatesqld);
        }
        if (reservationsIdx != null && reservationsIdx != "") {

        } else {
            reservationsIdx = reservationsId;
        }
        //修改储值余额
        String djmoney = "select  COALESCE(sum(actual_account_balance),0.00) as totalDjbalance from reservemoney_deal" +
                " where reservations_id='" + reservationsId + "' ";
        RechargeInfo djmone = sqlMapper.selectOne(djmoney, RechargeInfo.class);
        String sqldj2 = "update " + ShiroKit.getUser().getCustomer_info() + " set money_balance='" + djmone.getTotalDjbalance() +
                "' where reservations_id='" + reservationsId + "' ";
        sqlMapper.update(sqldj2);
        String sumSql_1 = "select if(sum(folding_pay) is null,0,sum(folding_pay)) as foldingPay,if(sum(service_actual_pay) is null,0,sum(service_actual_pay)) as serviceActualPay,if(sum(deposit_amount) is null,0,sum(deposit_amount)) as depositAmount," +
                "if(sum(arrears) is null,0,sum(arrears)) as arrears  from " + ShiroKit.getUser().getDeal_info() + " where reservations_id='" + reservationsId + "' " +
                " and deal_status='已结账' limit 1";
        PaymentInfo paymentInfo_1 = sqlMapper.selectOne(sumSql_1, PaymentInfo.class);
        String sumSql_2 = "select if(sum(folding_pay) is null,0,sum(folding_pay)) as foldingPay,if(sum(service_actual_pay) is null,0,sum(service_actual_pay)) as serviceActualPay,if(sum(deposit_amount) is null,0,sum(deposit_amount)) as depositAmount," +
                "if(sum(arrears) is null,0,sum(arrears)) as arrears  from " + ShiroKit.getUser().getDeal_info() + " where reservations_id='" + reservationsId + "' " +
                " and deal_status='已结账' and get_time='" + getTime + "' limit 1";
        PaymentInfo paymentInfo_2 = sqlMapper.selectOne(sumSql_2, PaymentInfo.class);
        //修改客户表缴费欠费
        String updateSqlcu = "update " + ShiroKit.getUser().getCustomer_info() + " a set a.account_arrears=" + paymentInfo_1.getArrears() + "," +
                " a.actual_account_balance=" + (paymentInfo_1.getServiceActualPay() + paymentInfo_1.getDepositAmount()) + " where a.reservations_id='" + reservationsId + "'";
        sqlMapper.update(updateSqlcu);
        String sqlright = "update " + ShiroKit.getUser().getRecharge_info() + " a set a.recharges_time=left( DATE_ADD(a.recharge_time,INTERVAL 1 DAY) ,10) " +
                " where right(a.recharge_time,8)>= '" + ShiroKit.getUser().getSttleAccountsTime() + "' and a.recharges_time is null;";
        sqlMapper.update(sqlright);
        String qlleft = "update " + ShiroKit.getUser().getRecharge_info() + " a set a.recharges_time=left(a.recharge_time,10) " +
                " where right(a.recharge_time,8)<'" + ShiroKit.getUser().getSttleAccountsTime() + "' and a.recharges_time is null;";
        sqlMapper.update(qlleft);
        VipLevelInfo vipLevelInfo = memberProductService.getVipLevel(reservationsIdx);
        String updateSql2 = "update reservations_info set deal_status='已结账',money=" + paymentInfo_1.getServiceActualPay() + ",vip_level_name_id='" + vipLevelInfo.getId() + "' " +
                " where id='" + reservationsId + "' ";
        sqlMapper.update(updateSql2);
        Double jfbalance = (paymentInfo_2.getServiceActualPay() + paymentInfo_2.getDepositAmount()) * vipLevelInfo.getIntegralFactor();
        String inupsql = "insert into score_deal (reservations_id,customer_name,customer_phone,score_balance,mode,remarks,add_people,add_store) values" +
                " ('" + reservationsIdx + "','" + customerInfo.getCustomerName() + "','" + customerInfo.getCustomerPhone() + "'," +
                " '" + jfbalance + "','积分变动','积分" + remarks + "'," +
                " '" + ShiroKit.getUser().getAccount() + "','" + ShiroKit.getUser().getOutpatientName() + "')";
        sqlMapper.insert(inupsql);
        String sesql1 = "select id,a.treservations_id as treservationsId,a.recommendation_name as recommendationName," +
                "a.recommendation_phone as recommendationPhone from member_recommendation_list_info a where a.reservations_id='" + reservationsIdx + "'" +
                "  and time between DATE_SUB(NOW(), INTERVAL 365 DAY) and DATE_SUB(NOW(), INTERVAL 0 DAY) limit 1";
        QueryInfo queryInfo = sqlMapper.selectOne(sesql1, QueryInfo.class);
        if (queryInfo != null) {
            String tinupsql = "INSERT INTO score_deal (`reservations_id`, `customer_name`, `customer_phone`, `score_balance`, `mode`, `remarks`, `add_people`,`add_store`)" +
                    " VALUES ('" + queryInfo.getTreservationsId() + "', '" + queryInfo.getRecommendationName() + "', '" + queryInfo.getRecommendationPhone() + "','" + jfbalance + "', '同推荐人', '同推荐人', '推荐人','" + ShiroKit.getUser().getOutpatientName() + "')";
            sqlMapper.insert(tinupsql);
        }
        return "1";
    }

    public int getQueryPagesCount(Page<RechargeInfo> page, SearchRechargeInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        String tableHead = searchInfo.getAbbreviation();
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            params.put("startTime", searchInfo.getStartTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            params.put("endTime", searchInfo.getEndTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDealStatus())) {
            params.put("dealStatus", searchInfo.getDealStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerStatus())) {
            params.put("customerStatus", searchInfo.getCustomerStatus());
        }
        if (!"请选择".equals(searchInfo.getOneChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getOneChannel())) {
                params.put("oneChannel", searchInfo.getOneChannel());
            }
        }
        if (!"请选择".equals(searchInfo.getTwoChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getTwoChannel())) {
                params.put("twoChannel", searchInfo.getTwoChannel());
            }
        }
        if (!"请选择".equals(searchInfo.getThrChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getThrChannel())) {
                params.put("thrChannel", searchInfo.getThrChannel());
            }
        }
        params.put("tableName", tableHead +"customer_info");
        params.put("tableName1", tableHead + "deal_info");
        params.put("tableName2", tableHead + "recharge_info");
        return dao.getQueryPagesCount(page, params);
    }

    @Transactional
    public void delete(String id) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "customer_info";
        String tableName1 = tableHead + "recharge_info";
        String selectSql = "select * from " + tableName1 + " where id = '" + id + "' limit 1";
        RechargeInfo rechargeInfo = sqlMapper.selectOne(selectSql, RechargeInfo.class);
        String updateSql = "update " + tableName + " a set a.actual_account_balance=actual_account_balance-'" + rechargeInfo.getActualAccountBalance() + "'," +
                " a.give_account_balance=give_account_balance-'" + rechargeInfo.getGiveAccountBalance() + "', " +
                " a.account_arrears=account_arrears-'" + rechargeInfo.getAccountArrears() + "' where reservations_id='" + rechargeInfo.getReservationsId() + "'";
        String sql = "delete from " + tableName1 + " where id = '" + id + "'";
        if ("是".equals(rechargeInfo.getIsRecord())) {
            sqlMapper.update(updateSql);
        }
        sqlMapper.delete(sql);
    }

    @Transactional
    public void updateGetTime(String reservationsId, String getTime, String time) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "deal_info";
        String updatesqltime = "update " + tableName1 + " set get_time='" + time + "' " +
                " where reservations_id='" + reservationsId + "' and get_time='" + getTime + "'";
        sqlMapper.update(updatesqltime);
    }

    public void deletes(String id) {
        String tableName = ShiroKit.getUser().getDeal_info();
        String selectSql = "select * from " + tableName + " where id = '" + id + "' limit 1";
        RechargeInfo rechargeInfo = sqlMapper.selectOne(selectSql, RechargeInfo.class);
        String deleteSql = "delete from " + tableName + " where customer_id='" + rechargeInfo.getCustomerId() + "' " +
                "and get_time='" + rechargeInfo.getGetTime() + "'";
        String insql="INSERT INTO `delete_order_info` (`delete_user`, `reservations_id`) VALUES " +
                "('"+ShiroKit.getUser().getName()+"', '"+rechargeInfo.getReservationsId()+"');";
        sqlMapper.insert(insql);
        sqlMapper.delete(deleteSql);
    }
}